//
//  BigStoryViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/31.
//

#import "BigStoryViewController.h"
#import "CommonUtil.h"
#import "TopView.h"
#import "StoryBottomView.h"
#import "ReplayViewController.h"
#import "StoryHeaderView.h"
#import "CommunityModel.h"
#import "CommunityTableViewCell.h"
#import "MessageModel.h"
#import "LeaingMessageTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "GalleryTopView.h"
#import "IQActionSheetPickerView.h"
#import "PageView.h"
#import "SegmentView.h"

@interface BigStoryViewController ()

@property (strong, nonatomic) SegmentView *segmentView;
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) UIScrollView *contentScrollView;
@property (strong, nonatomic) PageView *pageView;
@property (strong, nonatomic) GalleryTopView *galleryView;



@end

@implementation BigStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.view.backgroundColor = RGB(241.0, 241.0, 241.0);

    
    if (self.type == DetailPageTypeBigStory) {
        self.navigationItem.title = @"第035期";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_bigstory_more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightClick:)];
        [self.view addSubview:self.pageView];
        self.pageView.type = self.type;
    }else if (self.type == DetailPageTypeGallery){
        self.navigationItem.title = @"金旺旺信箱";
        
        [self.rightBtn setTitle:@"收藏" forState:UIControlStateNormal];
        [self.rightBtn setTitle:@"取消收藏" forState:UIControlStateSelected];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
        
        
        [self.view addSubview:self.galleryView];
        [self.view addSubview:self.contentScrollView];
        NSArray *titles = self.galleryView.titles;
        for (NSInteger i = 0; i < titles.count; i++) {
            PageView *view = [[PageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, self.contentScrollView.height)];
            view.superVC = self;
            view.type = self.type;
            [self.contentScrollView addSubview:view];
            self.contentScrollView.contentSize = CGSizeMake(titles.count * SCREEN_WIDTH, 0);
        }
        
    }else if (self.type == DetailPageTypeDog || self.type == DetailPageTypeHumor || self.type == DetailPageTypeNorFowl){
        
        
        if (self.type == DetailPageTypeDog) {
            self.navigationItem.title = @"跑狗玄机";
        }else if (self.type == DetailPageTypeHumor){
            self.navigationItem.title = @"幽默猜测";
        }else if (self.type == DetailPageTypeNorFowl){
            self.navigationItem.title = @"四不像";
        }
        
        [self.rightBtn setTitle:@"2021" forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
        
        [self.view addSubview:self.segmentView];
        [self.view addSubview:self.galleryView];
        [self.view addSubview:self.contentScrollView];
        self.galleryView.top = self.segmentView.bottom + AD(20);
        self.contentScrollView.top = self.galleryView.bottom;
        self.contentScrollView.height = SCREEN_HEIGHT - self.galleryView.bottom;
        
        NSArray *titles = self.galleryView.titles;
        for (NSInteger i = 0; i < titles.count; i++) {
            PageView *view = [[PageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, self.contentScrollView.height)];
            view.superVC = self;
            view.type = self.type;
            [self.contentScrollView addSubview:view];
            self.contentScrollView.contentSize = CGSizeMake(titles.count * SCREEN_WIDTH, 0);
        }
    }

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.pageView.headerView.timer invalidate];
    self.pageView.headerView.timer = nil;
}


- (void)inputBtnClick:(UIButton *)sender{
    [self presentViewController:[ReplayViewController new] animated:YES completion:nil];
}

- (void)rightBtnClick:(UIButton *)btn{
    
    if (self.type == DetailPageTypeGallery) {
        btn.selected = !btn.selected;
    }else if (self.type == DetailPageTypeDog || self.type == DetailPageTypeHumor || self.type == DetailPageTypeNorFowl){
        [self pickerBtnClick:btn];
    }
}

- (void)rightClick:(UIBarButtonItem *)item{
    
    // 1.创建UIAlertController
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                              message:nil
                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
    
    // 2.1 创建按钮
    UIAlertAction *findDetailAction = [UIAlertAction actionWithTitle:@"查看用户主页" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Delete Action");
    }];
    UIAlertAction *historyAction = [UIAlertAction actionWithTitle:@"历史帖子" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Delete Action");
    }];
    UIAlertAction *reportAction = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Delete Action");
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel Action");
    }];
    
    // 2.2 添加按钮
    [alertController addAction:findDetailAction];
    [alertController addAction:historyAction];
    [alertController addAction:reportAction];
    [alertController addAction:cancelAction];
    
    // 3.显示警报控制器
    [self presentViewController:alertController animated:YES completion:nil];
}




- (void)pickerBtnClick:(UIButton *)sender{
    IQActionSheetPickerView *pickerView = [[IQActionSheetPickerView alloc] initWithTitle:@"" delegate:self];
    pickerView.actionSheetPickerStyle = IQActionSheetPickerStyleTextPicker;
    pickerView.titlesForComponents = @[@[@"2021",@"2020",@"2020",@"2020",@"2020",@"2020",@"2020",@"2020"]];
    [pickerView show];
}

#pragma mark - lazy

-(UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                                                                            self.galleryView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-self.galleryView.bottom)];
        _contentScrollView.pagingEnabled = YES;
        if (@available(iOS 11.0, *)) {
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
    }
    return _contentScrollView;
}
-(PageView *)pageView{
    if (!_pageView) {
        _pageView = [[PageView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNavBarAndStatusBarHeight)];
        _pageView.superVC = self;
        [_pageView.bottomView.inputBtn addTarget:self action:@selector(inputBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pageView;
}
/// 期数选择
-(GalleryTopView *)galleryView{
    if (!_galleryView) {
        _galleryView = [[GalleryTopView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, AD(40))];
        [_galleryView.timeBtn addTarget:self action:@selector(pickerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _galleryView.type = self.type;
        __weak typeof(self) weakSelf = self;
        _galleryView.actionBlock = ^(NSInteger index) {
            [weakSelf.contentScrollView setContentOffset:CGPointMake(index * SCREEN_WIDTH, 0) animated:YES];
        };
    }
    return _galleryView;
}

-(UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _rightBtn.layer.cornerRadius = AD(3);
        _rightBtn.clipsToBounds = YES;
        _rightBtn.backgroundColor = RGB(255.0, 82.0, 122.0);
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
        [_rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.frame = CGRectMake(0, 0, AD(70), AD(25));
    }
    return _rightBtn;
}

-(SegmentView *)segmentView{
    if (!_segmentView) {
        _segmentView = [[SegmentView alloc] initWithFrame:CGRectMake(AD(20), kNavBarAndStatusBarHeight + AD(10), SCREEN_WIDTH-AD(40), AD(40))];
        if (self.type == DetailPageTypeDog) {
            _segmentView.titles = @[@"新版跑狗",@"旧版跑狗"];
        }else if (self.type == DetailPageTypeNorFowl){
            _segmentView.titles = @[@"四不像",@"香港挂牌"];
        }else if (self.type == DetailPageTypeHumor){
            _segmentView.titles = @[@"图片猜测",@"视频猜测"];
        }
        
    }
    return _segmentView;
}

@end
